package com.huawei.hwCloudJs.service.a;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.huawei.hwCloudJs.api.ValidateWhiteListListener;
import com.huawei.hwCloudJs.service.a.a.a;
import com.huawei.hwCloudJs.service.a.a.c;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public final class b {
    private static final String a = "AuthManager";
    private static ValidateWhiteListListener d;
    private static b f = new b();
    private final HashMap<String, a> b = new HashMap();
    private final ArrayList<String> c = new ArrayList();
    private final a e = new a();

    private b() {
    }

    public static b a() {
        return f;
    }

    public static void a(ValidateWhiteListListener validateWhiteListListener) {
        d = validateWhiteListListener;
    }

    private void a(a aVar) {
        this.e.a(aVar);
    }

    private void a(String str, a aVar) {
        synchronized (this.b) {
            this.b.put(str, aVar);
        }
    }

    private void b(String str, String str2, List<String> list, a aVar, Context context) {
        if (com.huawei.hwCloudJs.d.a.a()) {
            c cVar = (c) com.huawei.hwCloudJs.service.http.b.a(new com.huawei.hwCloudJs.service.a.a.b(str), context);
            if (cVar.f() == 1) {
                if (cVar.b() == 0) {
                    a aVar2 = new a();
                    aVar2.b(cVar.d());
                    aVar2.a(cVar.a());
                    aVar2.a(str);
                    aVar2.a(System.currentTimeMillis());
                    if (c.a(str2, list, aVar2)) {
                        aVar.a(0);
                        a(str, aVar2);
                        a(aVar2);
                        return;
                    }
                    aVar.a(-1);
                    return;
                }
                aVar.a(cVar.b());
                return;
            } else if (cVar.f() == 2) {
                aVar.a(cVar.e());
                return;
            } else if (cVar.f() == 3) {
                aVar.a(-3);
                return;
            } else {
                aVar.a(-4);
                return;
            }
        }
        aVar.a(-2);
    }

    private boolean c(String str) {
        if (d == null) {
            return false;
        }
        synchronized (this.c) {
            if (this.c.size() == 0) {
                return false;
            }
            Iterator it = this.c.iterator();
            while (it.hasNext()) {
                if (d.validate((String) it.next(), str)) {
                    return true;
                }
            }
            return false;
        }
    }

    public void a(String str) {
        synchronized (this.c) {
            if (!this.c.contains(str)) {
                this.c.add(str);
            }
        }
    }

    public void a(String str, String str2, List<String> list, a aVar, Context context) {
        if (a(str, str2, (List) list)) {
            aVar.a(0);
        } else {
            new Thread(new 1(this, str, str2, list, aVar, context)).start();
        }
    }

    public boolean a(String str, String str2, Context context) {
        c cVar = (c) com.huawei.hwCloudJs.service.http.b.a(new com.huawei.hwCloudJs.service.a.a.b(str), context);
        return cVar.f() == 1 && cVar.b() == 0 && c.a(cVar.d(), str2);
    }

    public boolean a(String str, String str2, List<String> list) {
        boolean z;
        synchronized (this.b) {
            a aVar = (a) this.b.get(str);
            z = aVar != null && c.a(str2, list, aVar);
        }
        return z;
    }

    public boolean b(String str) {
        Log.i(a, "JS isUrlWhileList");
        if (!TextUtils.isEmpty(str)) {
            return d != null ? c(str) : false;
        } else {
            Log.e(a, "JS isUrlWhileList isEmpty");
            return false;
        }
    }
}
